<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------

/**
 * Profile controller class
 *
 * This class is repsonsible for content on the front page. Extends the layout controller.
 *
 * @package		AchieveIt
 * @subpackage	Controllers
 * @category	Controllers
 * @author		Russ
 */
class Profile extends LoggedIn {
	
	/**
	 * Serves the home page
	 *
	 * @access	public
	 */
	public function index($username = '', $page = 'overview')
	{
		$this->load->model(array('Achievements_model', 'Friends_model', 'User_model'));
		$this->load->helper(array('profile_loader_helper','url'));
		
		$user = $this->session->all_userdata();
		
		/* If no username provided or username is invalid redirect to own profile */
		if($username == '' || $this->User_model->get_user_by_username($username) == NULL)
			redirect('/profile/' . $user['username']);
	
		/* If invalid page re-route to overview */
		if(!$this->pageExists($page))
			redirect('/profile/' . $username);
	
		/* Load user profile - just pass in the username */
		loadProfile($username, $page);
		
		$this->data['title'] = $username . "'s Profile";
		$this->data['page'] = $page;
                $address['imgsrc'] = "/_static/images/layout/logo.png";
                
		$this->parser->parse("profile/profile.tpl", $this->data);
                $this->load->view('profilePicture', $address);
	}
	
	/**
	 * If the page is a valid one, return true, false otherwise
	 */
	public function pageExists($page)
	{
		$page = strtolower($page);
		if($page == 'friends' || 
		   $page == 'todo' || 
		   $page == 'completed' || 
		   $page == 'overview')
			return true;
		return false;
	}
	
	/**
	 * Adds an achievement to a users TODO list
	 *
	 * @access	public
	 */
	public function add_todo($achievementId = '')
	{
		$this->load->helper(array('url'));
		$this->load->model(array('Achievements_model'));
		
		if($achievementId == '' || $this->Achievements_model->get_achievement_by_id($achievementId) == NULL)
		{
			redirect('/browse');
			return;
		}
		
		$user = $this->session->all_userdata();
		
		/* make sure the achievement hasn't already been completed */
		if($this->Achievements_model->get_completed_by_id($achievementId, $user['id']) != NULL)
		{
			redirect('/browse');
			return;
		}
		
		if(!$this->Achievements_model->todo_achievement_exists($achievementId, $user['id']))
		{
			$this->session->set_flashdata('notice', 'The selected achievement has been added to your TODO list');
			$this->Achievements_model->create_todo_achievement($achievementId, $user['id']);
		} else
			$this->session->set_flashdata('notice', 'The selected achievement already exists in your TODO list');
		redirect('/browse');
	}
	
	/**
	 * Removes an achievement from a users TODO list
	 *
	 * @access	public
	 */
	public function remove_todo($achievementId = '')
	{
		$this->load->helper(array('url'));
		$this->load->model(array('Achievements_model'));
		
		if($achievementId == '' || $this->Achievements_model->get_achievement_by_id($achievementId) == NULL)
		{
			redirect('/browse');
			return;
		}
		
		$user = $this->session->all_userdata();
		if($this->Achievements_model->todo_achievement_exists($achievementId, $user['id']))
		{
			$this->session->set_flashdata('notice', 'The selected achievement has been removed from your TODO list');
			$this->Achievements_model->delete_todo_achievement($achievementId, $user['id']);
		} else
			$this->session->set_flashdata('notice', 'The selected achievement does not exist in your TODO list');
		redirect('/profile');
	}	
	
}
?>